package com.iter.devbox.cassandra;

import java.util.List;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Host;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.TableMetadata;

public class CassandraClient {
	
	public static void main(String[] args) {
		Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(9042).build();
		Metadata metadata = cluster.getMetadata();
		for (Host host : metadata.getAllHosts()) {
			System.out.println(host.getAddress().getHostAddress());
		}
		
		System.out.println("===============================");
		
		for (KeyspaceMetadata ksm : metadata.getKeyspaces()) {
			System.out.println(ksm.getName());
			System.out.println(">>>>");
			for (TableMetadata tm : ksm.getTables()) {
				System.out.println(tm.getName());
			}
			System.out.println("-------------------------");
		}
		
		//创建 keyspace
		//需要获取session对象
		Session session = cluster.connect();
		String createKeyspaceCQL = "create keyspace if not exists mydevboxkeyspace with replication={'class':'SimpleStrategy', 'replication_factor':1}";
		session.execute(createKeyspaceCQL);
		
		//创建列族
		String createCloumFamilyCQL = "create table if not exists mydevboxkeyspace.student(id int primary key, name varchar, age int)";
		session.execute(createCloumFamilyCQL);
		
		//插入数据
		String insertCQL = "insert into mydevboxkeyspace.student(id, name, age) values(1, 'zhangsan', 20)";
		session.execute(insertCQL);
		
		//查询数据
		String queryCQL = "select * from mydevboxkeyspace.student";
		ResultSet rs = session.execute(queryCQL);
		List<Row> dataList = rs.all();
		for (Row row : dataList) {
			System.out.println("=>id: " + row.getInt("id"));
			System.out.println("=>name: " + row.getString("name"));
			System.out.println("=>age : " + row.getInt("age"));
		}
		
		//修改数据 (cassandra只能基于主键条件修改吗？)
		/*String updateCQL = "update mydevboxkeyspace.student set age=22 where id=1";
		session.execute(updateCQL);
		
		String queryCQL = "select * from mydevboxkeyspace.student";
		ResultSet rs = session.execute(queryCQL);
		List<Row> dataList = rs.all();
		for (Row row : dataList) {
			System.out.println("=>id: " + row.getInt("id"));
			System.out.println("=>name: " + row.getString("name"));
			System.out.println("=>age : " + row.getInt("age"));
		}*/

		//删除数据
		/*String deleteCQL = "delete from mydevboxkeyspace.student where id=1";
		String queryCQL = "select * from mydevboxkeyspace.student";
		session.execute(deleteCQL);
		ResultSet rs = session.execute(queryCQL);
		List<Row> dataList = rs.all();
		for (Row row : dataList) {
			System.out.println("=>id: " + row.getString("id"));
			System.out.println("=>name: " + row.getString("name"));
			System.out.println("=>age : " + row.getInt("age"));
		}*/
		
		//session.close();
		cluster.close();
	}
}
